﻿using Microsoft.EntityFrameworkCore;
using Zhaoxi.ReadingPlatform.BusinessInterface;
using Zhaoxi.ReadingPlatform.Models;

namespace Zhaoxi.ReadingPlatform.BusinessService
{
    public class BookService : BaseService, IBookService
    {
        /// <summary>
        /// 这里的构造函数将context交给父类的构造函数
        /// </summary>
        /// <param name="context"></param>
        public BookService(DbContext? context) : base(context) { }

        /// <summary>
        /// 获取图书章节
        /// </summary>
        /// <param name="finalChapter"></param>
        /// <param name="bookid"></param>
        /// <param name="chapterId"></param>
        /// <returns></returns>
        public BookChapterInfo? IndexBookChapterInfo(out int finalChapter, int bookid, int chapterId)
        {
            BookChapterInfo? bookChapterInfo = Context.Set<BookChapterInfo>()
                .AsNoTracking()// 查询的时候就别用跟踪了，提高性能。默认情况下是会跟踪的。
                .FirstOrDefault(c => c.BookId.Equals(bookid) && c.Chapter.Equals(chapterId));

            // 查询出一共有多少个章节编号
            finalChapter = Context.Set<BookChapterInfo>().OrderByDescending(c => c.Chapter).First().Chapter;

            return bookChapterInfo;
        }
    }
}
